package com.maiji.cloud.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.maiji.cloud.request.shopingmall.AddDownPriceUserLogReqDto;
import com.maiji.cloud.request.shopingmall.DownPriceDetailReqDto;
import com.maiji.cloud.request.shopingmall.DownPriceGoodsReqDto;
import com.maiji.cloud.response.BaseDataResDto;
import com.maiji.cloud.response.BaseResDto;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.response.shopingmall.DownPriceDetailResDto;
import com.maiji.cloud.response.shopingmall.DownPriceGoodsListResDto;
import com.maiji.cloud.response.shopingmall.DownPriceUserLogListResDto;
import com.maiji.cloud.response.shopingmall.UnderwayDownPriceGoodsData;
import com.maiji.cloud.service.DownPriceGoodsService;

/**
 * 
 * Description:  
 * @author zhanglr  
 * @date 2019年6月29日
 */
@RestController
@RequestMapping("/downPriceGoods/")
public class DownPriceGoodsController {
	Logger logger = LoggerFactory.getLogger(DownPriceGoodsController.class);
	@Autowired
	private DownPriceGoodsService  downPriceGoodsService;
	
	
	/**
	 * 结束发起的砍价活动
	 * @param param
	 * @return
	 */
	@PostMapping("endDownPriceGoods")
	public BaseResDto endDownPriceGoods(@RequestBody DownPriceDetailReqDto param) {
		logger.info("DownPriceGoodsController.endDownPriceGoods param is {}",JSON.toJSONString(param));
		
		return downPriceGoodsService.endDownPriceGoods(param);
	}
	
	/**
	 * 获得砍价免费拿商品列表
	 * @param param
	 * @return
	 */
	@PostMapping("getDownPriceGoodsList")
	public DownPriceGoodsListResDto getDownPriceGoodsList(@RequestBody DownPriceGoodsReqDto param,@RequestHeader("maijiToken") String maijiToken) {
		logger.info("DownPriceGoodsController.getDownPriceGoodsList param is {}",JSON.toJSONString(param));
		param.setMaijiToken(maijiToken);
		return downPriceGoodsService.getDownPriceGoodsList(param);
	}
	
	/**
	 * 获取砍价详情
	 * @param param
	 * @return
	 */
	@PostMapping("getDownPriceDetail")
	public DownPriceDetailResDto getDownPriceDetail(@RequestBody DownPriceDetailReqDto param,@RequestHeader(value="maijiToken",required =false) String maijiToken){
		logger.info("DownPriceGoodsController.getDownPriceDetail ,param is {}",JSON.toJSONString(param));
		param.getData().setMaijiToken(maijiToken);
		if(StringUtils.isEmpty(param.getData().getGoodId())) {
			return new DownPriceDetailResDto(Status.PARAMETERERROR);
		}
		if(StringUtils.isBlank(maijiToken) && StringUtils.isBlank(param.getData().getDownPriceGoodsLogId()) ) {
			return new DownPriceDetailResDto(Status.PARAMETERERROR);
		}
		
		return downPriceGoodsService.getDownPriceDetail(param);
	}
	/**
	 * 获取微信用户砍去的价格记录
	 * @param param
	 * @return
	 */
	@PostMapping("getDownPriceUserLogList")
	public DownPriceUserLogListResDto getDownPriceUserLogList(@RequestBody DownPriceDetailReqDto param){
		logger.info("DownPriceGoodsController.getDownPriceUserLogList ,param is {}",JSON.toJSONString(param));
		if(StringUtils.isEmpty(param.getData().getGoodId())) {
			return new DownPriceUserLogListResDto(Status.PARAMETERERROR);
		}
		
		return downPriceGoodsService.getDownPriceUserLogList(param);
	}
	/**
	 * 用户砍价
	 * @param param
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("addDownPriceUserLog")
	public BaseResDto addDownPriceUserLog(@RequestBody AddDownPriceUserLogReqDto param,@RequestHeader("maijiToken") String maijiToken){
		logger.info("DownPriceGoodsController.addDownPriceUserLog ,param is {}",JSON.toJSONString(param));
		
		param.getData().setMaijiToken(maijiToken);
		return downPriceGoodsService.addDownPriceUserLog(param);
	}
	/**
	 * 用户砍价正在进行中的商品列表
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("userUnderwayDownPriceGoods")
	public BaseDataResDto<List<UnderwayDownPriceGoodsData>> userUnderwayDownPriceGoods(@RequestHeader("maijiToken") String maijiToken){
		logger.info("DownPriceGoodsController.userUnderwayDownPriceGoods ,maijiToken is {}",maijiToken);
		return downPriceGoodsService.userUnderwayDownPriceGoods(maijiToken);
	}
	
}
